package com.formiaomiao.demain.controller;

import com.formiaomiao.demain.model.ComplaintBean;
import com.formiaomiao.demain.model.ComplaintImgBean;
import com.formiaomiao.demain.model.ImgBean;
import com.formiaomiao.demain.service.ComplaintService;
import com.formiaomiao.demain.utils.QiniuUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import java.io.FileInputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

@Controller
public class ComplaintController {

    @Autowired
    ComplaintService complaintService;

    QiniuUtil qiniuUtil = new QiniuUtil();

    /**
     * 投诉
     * @param complaintimg
     * @param userid
     * @param complaint
     * @param mac
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/insertcomplaint")
    private Object InsertComplaint(@RequestParam(name = "complaintimg", required = false) MultipartFile[] complaintimg,
                                   @RequestParam(name = "toid", required = false) Integer toid,
                                   @RequestParam(name = "userid", required = false) Integer userid,
                                   @RequestParam(name = "complaint", required = false) String complaint,
                                   @RequestParam(name = "mac", required = false) String mac){
        String uid = UUID.randomUUID().toString();
        ComplaintBean bean = new ComplaintBean();
        bean.setComplaintid(uid);
        bean.setToid(toid);
        bean.setUserid(userid);
        bean.setComplaint(complaint);
        bean.setMac(mac);
        List<ComplaintImgBean> urllist = new ArrayList<>();
        ComplaintImgBean imgBean = null;
        FileInputStream inputStream = null;
        for (int i = 0; i < complaintimg.length; i++) {
            imgBean = new ComplaintImgBean();
            try {
                inputStream = (FileInputStream) complaintimg[i].getInputStream();
                imgBean.setComplaintid(uid);
                String usl = qiniuUtil.uploadImg(inputStream, "complaintimg" + i + uid);
                imgBean.setImgurl(usl);
            } catch (IOException e) {
                e.printStackTrace();
            }
            urllist.add(imgBean);
        }
        bean.setComplaintImgBean(urllist);
        return complaintService.InsertComplaint(bean);
    }

}
